package studio.raptor.ddal.dashboard.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import studio.raptor.ddal.dashboard.log.bo.ConnPoolLogExtendData;
import studio.raptor.ddal.dashboard.log.bo.SqlProcessLogExtendData;
import studio.raptor.ddal.dashboard.log.repository.ConnPoolLogRepository;
import studio.raptor.ddal.dashboard.log.repository.SqlProcessLogRepository;
import studio.raptor.ddal.dashboard.service.interfaces.DdalMonitorService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/1/8.
 */
@Service
public class DdalMonitorServiceImpl implements DdalMonitorService {

    private static Logger log = LoggerFactory.getLogger(DdalMonitorServiceImpl.class);
    @Autowired
    private SqlProcessLogRepository sqlProcessLogRepository;
    @Autowired
    private ConnPoolLogRepository connPoolLogRepository;



    public List<SqlProcessLogExtendData> getSqlProcessLogData(String acceptBeginTime, String acceptEndTime, String appName, String sqlType, String processTime,String success,int page, int pageVolume){
        try {
            Pageable pageable = new PageRequest(page, pageVolume, Sort.Direction.DESC,"acceptTime");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            /*if (StringUtils.isNotEmpty(sqlType)) {
                return sqlProcessLogRepository.findByAcceptTimeBetweenAndAppNameAndSqlType(sdf.parse(acceptBeginTime), sdf.parse(acceptEndTime), appName, sqlType, pageable);
            } else {
                return sqlProcessLogRepository.findByAcceptTimeBetweenAndAppName(sdf.parse(acceptBeginTime), sdf.parse(acceptEndTime), appName, pageable);
            }*/
            Long p=StringUtils.isEmpty(processTime)?null:Long.valueOf(processTime);
            LogSqlProcessSpecification spec = new LogSqlProcessSpecification(sdf.parse(acceptBeginTime), sdf.parse(acceptEndTime), appName, sqlType,p,success);
            Page<SqlProcessLogExtendData> pages=sqlProcessLogRepository.findAll(spec,pageable);
            List<SqlProcessLogExtendData> list=null;
            if(pages!=null) {
                list = pages.getContent();
            }
            return list;
        }catch(ParseException e){
            log.error("日期串解析异常",e);
        }
        return null;
    }

    @Override
    public List<ConnPoolLogExtendData> getConnPoolLogData(String acceptBeginTime, String acceptEndTime, String appName, String username, int page, int pageVolume) {
        try {
            Pageable pageable = new PageRequest(page, pageVolume, Sort.Direction.DESC,"acceptTime");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotEmpty(username)) {
                List<ConnPoolLogExtendData> dataList=connPoolLogRepository.findByAcceptTimeAndAppNameAndUsername(sdf.parse(acceptBeginTime), sdf.parse(acceptEndTime), appName, username, pageable);
                return dataList;
            } else {
                List<ConnPoolLogExtendData> dataList=connPoolLogRepository.findByAcceptTimeAndAppName(sdf.parse(acceptBeginTime), sdf.parse(acceptEndTime), appName, pageable);
                return dataList;
            }
        }catch(ParseException e){
            log.error("日期串解析异常",e);
        }
        return null;
    }
}
